<?php
include_once "wxBizDataCrypt.php";

$code = $_POST["code"];
$iv = $_POST["iv"];
$encryptedData = $_POST["encryptedData"];
$appid = "wxa825ebeca9e83efc";
$secret = "3509ce78b2f8f329431b29be500cdaa0";

//获取session_key
$res = file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$code."&grant_type=authorization_code");
$res_arr = json_decode($res, true);
$session_key = $res_arr["session_key"];
$openid = $res_arr["openid"];

//解析手机号
$pc = new WXBizDataCrypt($appid, $session_key);
$errCode = $pc->decryptData($encryptedData, $iv, $data);
$result = array(
	'errcode' => $errCode,
	'openid' => '',
	'phoneNumber' => '',
);
if ($errCode == 0) {
	$data_arr = json_decode($data, true);
	$result["openid"] = $openid;
	$result["phoneNumber"] = $data_arr["phoneNumber"];
}
echo json_encode($result);